/**
 * @(#)LoginActivity.java        1.0 2/22/12
 * 
 * Copyright (c) 2012 public static droid main
 * Georgia Institute of Technology, Atlanta, GA
 * All rights reserved.
 */

package edu.gatech.oad.publicstaticdroidmain.taskmanager.ui;


import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.LoginManager;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.TaskManager;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.UserAccount;

/**
 * An Android activity that allows a user to log in.  Contains
 * a form for entering login credentials and a button that allows
 * a user to create an account.
 * 
 * @version 1.0 Feb 22 2012
 * @author Raheel Merali
 */
public class LoginActivity extends AbstractTaskManagerActivity {

    private OnClickListener buttonListener;

    private TextView usernameTextField;
    private TextView passwordTextField;
    private Button loginButton;
    private Button createAccountButton;

    private LoginManager loginManager;

    /**
     * Sets up the layout, captures the form elements into Java objects, and
     * adds listeners.
     * 
     * @param savedInstanceState saved data to reload
     */
    @SuppressWarnings("static-access")
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.login);

	usernameTextField = (TextView) findViewById(R.id.loginUsernameTextField);
	passwordTextField = (TextView) findViewById(R.id.loginPasswordTextField);
	loginButton = (Button) findViewById(R.id.loginLoginButton);
	createAccountButton = (Button) findViewById(R.id.loginCreateAccountButton);

	buttonListener = new LoginActivityButtonListener();

	createAccountButton.setOnClickListener(buttonListener);
	loginButton.setOnClickListener(buttonListener);

	loginManager = ((TaskManagerApplication)getApplication()).getLoginManager();
    }

    /**
     * Returns false so that a menu will not be displayed when the user
     * clicks the menu button.
     */
    public boolean onCreateOptionsMenu(Menu menu) {
	return false;
    }

    /**
     * Determines if user inputs correspond to a valid existing account.  Displays
     * an error toast if the credentials are invalid.
     * 
     * @param username
     * @param password
     * @return true if the credentials are valid; false otherwise
     */
    private boolean validateInputsAndShowErrorMessages(String username, String password) {
	if (!loginManager.verifyCredentials(username, password)) {
	    showErrorToast(getString(R.string.login_invalid_credentials_message));
	    return false;
	}

	return true;
    }

    /**
     * A listener for button clicks.  Handles the actual logging in by retrieving
     * a UserAccount object from the database upon a successful login.
     *
     */
    private class LoginActivityButtonListener implements OnClickListener {
	@SuppressWarnings("static-access")
	public void onClick(View view)
	{
	    switch(view.getId()) {
	    case R.id.loginLoginButton:

		String username = usernameTextField.getText().toString();
		String password = passwordTextField.getText().toString();

		if (!validateInputsAndShowErrorMessages(username, password)) {
		    break;
		}

		UserAccount account = loginManager.login(username, password);
		((TaskManagerApplication)(getApplication())).setTaskManager(new TaskManager(account));
		startUIActivity(getString(R.string.activity_task_view));

		break;

	    case R.id.loginCreateAccountButton:
		startUIActivity(getString(R.string.activity_create_account));
		break;

	    } //end switch
	} //end onClick
    } //end LoginActivityButtonListener

}